#pragma once
#include <memory>
#include <sys/types.h>
#include <sys/socket.h>
#include "Connection.hpp"
class HandlerConnection
{
public:
    HandlerConnection(func_t func) : _func(func)
    {}
    void HandlerRecv(std::shared_ptr<Connection> conn)
    {
        // DLOG("HandlerRecv fd : %d" , conn->Sockfd());
        while(true)
        {
            char buffer[1024];
            ssize_t n = recv(conn->Sock()->Sockfd(),buffer,sizeof(buffer),0);
            if(n > 0)
            {
                buffer[n] = 0;
                conn->AppendInBuffer(buffer);
            }
            else
            {
                if(errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    break;
                }
                else if(errno == EINTR)
                {
                    continue;
                }
                else 
                {
                    conn->_excepter(conn);
                    return;
                }
            }
        }
        _func(conn);
    }
    void HandlerSend(std::shared_ptr<Connection> conn)
    {
    }
    void HanlderExcept(std::shared_ptr<Connection> conn)
    {
    }
    private:
        func_t _func;
};